package com.hdu.controller.mediaManagement;


import com.hdu.dto.QueryMediaParamsDto;
import com.hdu.entity.MediaFiles;
import com.hdu.exception.BizException;
import com.hdu.pojo.PageBean;
import com.hdu.pojo.Result;
import com.hdu.service.IMediaFileService;
import com.hdu.vo.UploadFileResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 媒资文件管理接口
 * </p>
 *
 * @author xiaobai
 * @since 2024-05-20
 */

@Slf4j
@RestController
@RequiredArgsConstructor
@Api(value = "媒资文件管理接口", tags = "媒资文件管理接口")
public class MediaFilesController {

    private final IMediaFileService mediaFileService;
    private final RestTemplate restTemplate;


    @ApiOperation("媒资列表查询接口")
    @PostMapping("/files")
    public Result<PageBean<MediaFiles>> list(@RequestBody QueryMediaParamsDto queryMediaParamsDto) {
        Long companyId = 1232141425L;
        return Result.success(mediaFileService.queryMediaFiles(companyId, queryMediaParamsDto));

    }

    @ApiOperation("上传文件")
    @RequestMapping(value = "/upload/courseFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public UploadFileResultVo upload(@RequestPart("fileData") MultipartFile upload,
                                     @RequestParam(value = "tag", required = false) String tag,
                                     @RequestParam(value = "folder", required = false) String folder,
                                     @RequestParam(value = "objectName", required = false) String objectName) {

        try {
            return mediaFileService.uploadFile(upload, folder, objectName, tag);
        } catch (IOException e) {
            throw new BizException("上传文件过程出错:" + e.getMessage());
        }
    }


    @ApiOperation(value = "预览文件")
    @GetMapping("/preview/{mediaId}")
    public Result<String> getPlayUrlByMediaId(@PathVariable String mediaId) {
        MediaFiles mediaFile = mediaFileService.getFileById(mediaId);
        String url = "http://192.168.31.254:9090" + mediaFile.getUrl();
        return Result.success(url);
    }
}
